package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.OperatorLog;
import com.qian.api.ddj.model.OperatorLogQuery;
import com.qian.api.ddj.service.IOperatorLogService;
import com.qian.service.ddj.dao.OperatorLogRepository;
import com.qian.service.ddj.entity.DdjOperatorLog;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.Date;

@Service
public class OperatorLogServiceImpl extends BaseServiceImpl<OperatorLog, DdjOperatorLog, Long> implements IOperatorLogService {

    @Autowired
    private OperatorLogRepository operatorLogDao;
    @Autowired
    private PageUtils pageUtils;

    @Override
    protected BaseRepository getDao() {
        return operatorLogDao;
    }

    @Override
    protected DdjOperatorLog model2Entity(OperatorLog model) {
        DdjOperatorLog ddjOperatorLog = null;
        if (isNotEmpty(model)) {
            ddjOperatorLog = new DdjOperatorLog();
            ddjOperatorLog.setOperator(model.getOperator());
            ddjOperatorLog.setUserId(model.getUserId());
            ddjOperatorLog.setId(model.getId());
            ddjOperatorLog.setUsername(model.getUsername());
        }
        return ddjOperatorLog;
    }

    @Override
    protected OperatorLog entity2model(DdjOperatorLog entity) {
        OperatorLog operatorLog = null;
        if (isNotEmpty(entity)) {
            operatorLog = new OperatorLog();
            operatorLog.setCreateTime(entity.getCreateTime());
            operatorLog.setOperator(entity.getOperator());
            operatorLog.setUserId(entity.getUserId());
            operatorLog.setId(entity.getId());
            operatorLog.setUsername(entity.getUsername());
        }
        return operatorLog;
    }

    @Override
    public OperatorLog save(OperatorLog model) {
        return super.simpleSave(model);
    }

    @Override
    public PageResp<OperatorLog> findAll(OperatorLogQuery query, PageReq pageReq) {
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq, new Sort(Sort.Direction.DESC, "id"));
        Page page = getDao().findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Date> timePath = root.get("createTime");
                Predicate condition = criteriaBuilder.isNotNull(root.<Integer>get("id"));
                // 日期比较
                if (isNotEmpty(query.getBeginTime()) && isNotEmpty(query.getEndTime()))
                    condition = criteriaBuilder.and(condition, criteriaBuilder.between(timePath, query.getBeginTime(), query.getEndTime()));
                else if (isNotEmpty(query.getBeginTime()))
                    condition = criteriaBuilder.and(condition, criteriaBuilder.greaterThanOrEqualTo(timePath, query.getBeginTime()));
                else if (isNotEmpty(query.getEndTime()))
                    condition = criteriaBuilder.and(condition, criteriaBuilder.lessThanOrEqualTo(timePath, query.getEndTime()));
                if (isNotEmpty(query.getUserId())) {
                    condition = criteriaBuilder.and(condition, criteriaBuilder.equal(root.get("userId"), query.getUserId()));
                }
                return condition;
            }
        }, pageRequest);
        return page2pageResp(page);
    }
}
